<?php

namespace App\Http\Controllers\Admin;;

use App\Model\AdminPermission;
use App\Model\AdminRoleUser;
use App\Model\AdminUser;
use App\Model\Branch;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class UserController extends BaseController
{
    /*
     * 用户列表
     */
    public function index()
    {
        $user =   Auth::user();

        if($user->is_branch != 0){
            $branch = Branch::where('id',$user['is_branch'])->first();
            $users = \App\Model\AdminUser::where('status', '=', '1')->where( 'is_branch', '=',$branch['id'])->paginate(20);
        } else {
            $users = \App\Model\AdminUser::where('status','>',0)->paginate(20);
        }
        return view('Admin.user.index', compact('users'));

    }

    /*
     * 创建用户
     */
    public function create()
    {
        return view('Admin.user.add');
    }

    /*
     * 创建用户
     */
    public function store(Request $request)
    {
        //密码
        $password = bcrypt(request('password'));
        $data = $request->all();
        if($data['user']){
            //查询用户名
            $user = AdminUser::where(['name'=>$data['user']['name']])->count();
            //是否注册
            if (!empty($user)) {
                return ['code'=>0,'data'=>'已经被注册'];
            }
            //用户名
            if(!$data['user']['name']){
                return ['code' => 0, 'data' => '用户名不能为空'];
            }
            //密码
//            if (!preg_match("/^[\d]{6,16}$/",$data['user']['password'])) {
//                return ['code' => 0, 'data' => '请填写至少6位以上的密码'];
//            }
            //姓名
            if(!$data['user']['user_name']){
                return ['code' => 0, 'data' => '姓名不能为空'];
            }
            //电话
            if (!preg_match("/^1[3456789]\d{9}$/", $data['user']['phone'])) {
                return ['code' => 0, 'data' => '请填写正确的手机号'];
            }
            //邮箱
            if (!preg_match("/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/",$data['user']['emil'])) {
                return ['code' => 0, 'data' => '邮箱格式不正确'];
            }
            //职位
            if(!$data['user']['position']){
                return ['code' =>0, 'data' => '职位不能为空'];
            }
            if($data['user']['is_branch']=='0'){
                $data['user']['type'] = 0;
            } else {
                $data['user']['type'] = 1;
            }
            //加密密码
             $data['user']['password'] =bcrypt($data['user']['password']);
            //注册时间
            $data['user']['sign_in'] = time();
            //状态
            $data['user']['status'] = 1;
            $Row = AdminUser::create($data['user']);
            if($Row){
                if(Auth::user()->is_branch!=0){
                    $data['perm']['role_id'] = 7;
                    $data['perm']['user_id'] = $Row->id;
                    AdminRoleUser::insert($data['perm']);
                }
                return ['code' => '1' , 'data' => '添加成功'];
            } else{
                return ['code' => '0' , 'data' => '添加失败'];
            }
        }
    }

    /*
     * 修改用户信息
     */
    public function editUser(Request $request)
    {
        $data = $request->all();
        switch ($data['class']){
            //查询单条用信息
            case 'find':
                $userInfo = AdminUser::where('id',$data['id'])->first();
                if($userInfo){
                    return ['code' => 1, 'data' => $userInfo];
                } else {
                    return ['code' => 0, 'data' => '没有数据'];
                }
                break;
             // 修改用户信息
            case 'edit':
                if($data['userDetail']){
                    //密码
                    if (!preg_match("/^[a-zA-Z0-9]{6,16}$/",$data['userDetail']['password'])) {
                        return ['code' => 0, 'data' => '请填写至少6位以上的密码'];
                    }
                    //姓名
                    if(!$data['userDetail']['user_name']){
                        return ['code' => 0, 'data' => '姓名不能为空'];
                    }
                    //电话
                    if (!preg_match("/^1[3456789]\d{9}$/", $data['userDetail']['phone'])) {
                        return ['code' => 0, 'data' => '请填写正确的手机号'];
                    }
                    //邮箱
                    if (!preg_match("/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/",$data['userDetail']['emil'])) {
                        return ['code' => 0, 'data' => '邮箱格式不正确'];
                    }
                    //职位
                    if(!$data['userDetail']['position']){
                        return ['code' =>0, 'data' => '职位不能为空'];
                    }
                    //加密
                    $data['userDetail']['password'] = bcrypt($data['userDetail']['password']);
                    $Row = AdminUser::where('id',$data['id'])->update(array_only($data['userDetail'],['password','user_name','phone','emil','position']));
                    if($Row){
                        return ['code' => 1, 'data' => '修改成功'];
                    } else {
                        return ['code' => 0, 'data' => '修改失败'];
                    }
                }
                break;
        }
    }

    /*
     * 角色的权限
     */
    public function role(\App\Model\AdminUser $user)
    {
        $roles = \App\Model\AdminRole::all();
        $myRoles = $user->roles;
        return view('Admin.user.role', compact('roles', 'myRoles', 'user'));
    }

    /*
     * 保存权限
     */
    public function storeRole(\App\Model\AdminUser $user)
    {
      

        $roles = \App\Model\AdminRole::find(request('roles'));
        $myRoles = $user->roles;

        // 对已经有的权限
        $addRoles = $roles->diff($myRoles);
        foreach ($addRoles as $role) {
            $user->roles()->save($role);
        }

        $deleteRoles = $myRoles->diff($roles);
        foreach ($deleteRoles as $role) {
            $user->deleteRole($role);
        }
        return back();
    }
}
